package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.*;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "调度页面控制器")
@CrossOrigin
public class DispatchController {

    private static Logger logger = LoggerFactory.getLogger(DispatchController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaPlanService duolaPlanService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private DuolaTransOrderService duolaTransOrderService;
    @Autowired
    private DuolaLineService duolaLineService;


    /**
     * 添加生产计划
     *
     * @return
     */
    @PostMapping("/planAdd")
    @ApiOperation("添加生产计划")
    @ResponseBody
    public BaseResult planAdd(@RequestBody PlanAddDto planAddDto) {
        try {
            duolaPlanService.planAdd(planAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 根据客户id选出已绑定合同的工程
     *
     * @return
     */
    @GetMapping("/selectProjectByCustomerId")
    @ApiOperation("根据客户id选出已绑定合同的工程")
    @ResponseBody
    public BaseResult selectProjectByCustomerId(Integer customerId) {
        try {
          List<SelectProjectByCustomerIdVo>  list =  duolaPlanService.selectProjectByCustomerId(customerId);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 生产计划列表
     *
     * @return
     */
    @GetMapping("/planList")
    @ApiOperation("生产计划列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PlanListVo.class)
    })
    public BaseResult planList(String projectName,Long time,Integer orderStatus) {
        try {
            List<PlanListVo> list = duolaPlanService.planList(projectName,time,orderStatus);
            return RetResponse.makeOKRsp(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 生产计划管理列表
     *
     * @return
     */
    @GetMapping("/planManageList")
    @ApiOperation("生产计划管理列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PlanManageListVo.class)
    })
    public BaseResult planManageList(PlanManageListDto projectName) {
        try {
            PlanManageListVo vo = duolaPlanService.planManageList(projectName);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 修改生产计划
     *
     * @return
     */
    @PostMapping("/planEdit")
    @ApiOperation("修改生产计划")
    @ResponseBody
    public BaseResult planEdit(@RequestBody PlanEditDto planEditDto) {
        try {
            duolaPlanService.planEdit(planEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 生产管理计划开始发货
     *
     * @return
     */
    @PostMapping("/startSend")
    @ApiOperation("生产管理计划开始发货")
    @ResponseBody
    public BaseResult startSend(Integer id) {
        try {
            duolaPlanService.planSend(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 完成生产计划
     *
     * @return
     */
    @PostMapping("/planFinish")
    @ApiOperation("完成生产计划")
    @ResponseBody
    public BaseResult planFinish(@RequestBody PlanFinishDto planFinishDto) {
        try {
            duolaPlanService.planFinish(planFinishDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 修改生产计划跳转
     *
     * @return
     */
    @GetMapping("/planEditJump")
    @ApiOperation("生产计划详情/编辑跳转")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PlanEditJumpVo.class)
    })
    public BaseResult planEditJump(Integer id) {
        try {
            PlanEditJumpVo vo = duolaPlanService.planEditJump(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 删除生产计划
     *
     * @return
     */
    @GetMapping("/delPlan")
    @ApiOperation("删除生产计划")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PlanEditJumpVo.class)
    })
    public BaseResult delPlan(Integer id) {
        try {
            duolaPlanService.delPlan(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 获取所有车辆信息
     *
     * @return
     */
    @GetMapping("/getCarList")
    @ApiOperation("获取所有车辆信息")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = GetCarListVo.class)
    })
    public BaseResult getCarList(Integer planId) {
        try {
            List<GetCarListVo> vo = duolaPlanService.getCarList(planId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 发货
     *
     * @return
     */
    @PostMapping("/sendGoods")
    @ApiOperation("发货")
    @ResponseBody
    public BaseResult sendGoods(@RequestBody  SendGoodsDto sendGoodsDto) {
        try {
            duolaTransOrderService.sendGoods(sendGoodsDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 生产线列表
     *
     * @return
     */
    @GetMapping("/lineList")
    @ApiOperation("生产线列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = LineListVo.class)
    })
    public BaseResult lineList() {
        try {
            List<LineListVo> listVos = duolaLineService.lineList();
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 运输单列表
     *
     * @return
     */
    @GetMapping("/TransList")
    @ApiOperation("运输单列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TransListVo.class)
    })
    public BaseResult TransList(Integer planId, String carNo) {
        try {
            List<TransListVo> listVos = duolaTransOrderService.TransList(planId, carNo);
            return RetResponse.makeOKRsp(listVos);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 装料
     *
     * @return
     */
    @PostMapping("/loadGoods")
    @ApiOperation("装料")
    @ResponseBody
    public BaseResult loadGoods(@RequestBody LoadGoodsDto loadGoodsDto) {
        try {
            duolaTransOrderService.loadGoods(loadGoodsDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 装料跳转
     *
     * @return
     */
    @GetMapping("/loadGoodsJump")
    @ApiOperation("装料跳转")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = LoadGoodsJumpVo.class)
    })
    public BaseResult loadGoodsJump(Integer id) {
        try {
            LoadGoodsJumpVo vo = duolaTransOrderService.loadGoodsJump(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 装料取消
     *
     * @return
     */
    @PostMapping("/loadGoodsCancel")
    @ApiOperation("装料取消")
    @ResponseBody
    public BaseResult loadGoodsCancel(@RequestBody LoadGoodsCancelDto loadGoodsCancelDto) {
        try {
            duolaTransOrderService.loadGoodsCancel(loadGoodsCancelDto.getTransId(),loadGoodsCancelDto.getId());
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 通知车辆
     *
     * @return
     */
    @PostMapping("/noticeCar")
    @ApiOperation("通知车辆")
    @ResponseBody
    public BaseResult noticeCar(@RequestBody NoticeCarDto noticeCarDto) {
        try {
            duolaTransOrderService.noticeCar(noticeCarDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 装料排序
     *
     * @return
     */
    @PostMapping("/loadGoodsSort")
    @ApiOperation("装料排序")
    @ResponseBody
    public BaseResult loadGoodsSort(@RequestBody LoadGoodsSortDto loadGoodsSortDto) {
        try {
            duolaTransOrderService.loadGoodsSort(loadGoodsSortDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 运输完成
     *
     * @return
     */
    @PostMapping("/transFinish")
    @ApiOperation("运输完成")
    @ResponseBody
    public BaseResult transFinish(@RequestBody TransFinishDto transFinishDto) {
        try {
            duolaTransOrderService.transFinish(transFinishDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 退料转料
     *
     * @return
     */
    @PostMapping("/returnTransfer")
    @ApiOperation("退料转料")
    @ResponseBody
    public BaseResult returnTransfer(@RequestBody ReturnTransferDto returnTransferDto) {
        try {
            duolaTransOrderService.returnTransfer(returnTransferDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 退料转料跳转
     *
     * @return
     */
    @GetMapping("/returnTransferJump")
    @ApiOperation("退料转料跳转")
    @ResponseBody
    public BaseResult returnTransferJump(Integer transId) {
        try {
            ReturnTransferJumpVo vo =   duolaTransOrderService.returnTransferJump(transId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 推荐车辆
     *
     * @return
     */
    @GetMapping("/recommendCar")
    @ApiOperation("推荐车辆")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = RecommendCarVo.class)
    })
    public BaseResult recommendCar(Integer planId, Integer sendType, Double volume) {
        try {
            RecommendCarVo vo = duolaTransOrderService.recommendCar(planId, sendType, volume);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 获取所有空闲可用的车辆
     *
     * @return
     */
    @GetMapping("/getAllFreeCarList")
    @ApiOperation("获取所有空闲可用的车辆 传入方量,车牌号  不传显示所有")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = GetAllFreeCarListVo.class)
    })
    public BaseResult getAllFreeCarList(String carNo, Double volume) {
        try {
            List<GetAllFreeCarListVo> vo = duolaTransOrderService.getAllFreeCarList(carNo, volume);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

}
